package com.exchange.util;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.exchange.constant.BizConst;
import com.exchange.plugin.threadpool.ThreadPoolKit;
import com.jfinal.kit.PropKit;
import java.util.Map;

/**
 * 邮件发送工具类
 *
 * @author ml
 * @version 1.0
 * @create 2019-04-29 7:55 PM
 **/
public class EmailUtils {

	private static final String SIGN = "sign";    
    private static final String param = "code";
    
    /**
     * 实际发送
     * @param type
     * @param email
     * @param title
     * @param values
     */
    public static boolean sendMessage(String type, String email, String title, String value) {
    	
        String content = StrUtil.EMPTY;
        switch (type) {
        	// 注册 验证 模板
            case BizConst.SMS_TYPE_REGISTER :
                content = PropKit.get("register-content");
                break;
            // 忘记密码 验证 模板
            case BizConst.SMS_TYPE_CIPHER :
                content = PropKit.get("cipher-content");
                break;
            // 修改资金密码 验证 模板
            case BizConst.SMS_TYPE_PAY_CIPHER :
                content = PropKit.get("paycipher-content");
                break;
            // 绑定邮箱或电话 验证 模板
            case BizConst.SMS_TYPE_BINGDING :
                content = PropKit.get("binding-content");
                break;
            default:
                break;
        }
        if(StrUtil.isBlank(content)) {
        	return false;
        }
        content = StrUtil.replace(content, SIGN, PropKit.get("smssign"), Boolean.TRUE);
        content = StrUtil.replace(content, param, value, Boolean.TRUE);
        
        title = StrUtil.concat( Boolean.FALSE, "【", PropKit.get("smssign"),"】", title);

        Map<String, Object> map = MapUtil.newHashMap();
        map.put("from", PropKit.get("emailfrom"));
        map.put("to", email);
        map.put("appid", PropKit.get("emailappid"));
        map.put("subject", title);
        map.put("signature", PropKit.get("emailappkey"));
        map.put("text", content);
        
        ThreadPoolKit.submit(new Runnable() {
			public void run() {
				HttpUtil .post( PropKit.get("emailurl"), map);
			}
        });
        
		return true;
    
    }


}
